<?php

App::uses('AppController', 'Controller');

/**
 * Posts Controller
 *
 * @property Post $Post
 */
class PostsController extends AppController {

    public $paginate = array(
        'limit' => 10,
        'order' => array(
            'Post.id' => 'desc'
        )
    );

    /**
     * index method
     *
     * @return void
     */
    public function admin_index() {
        $this->Post->recursive = 0;
        $this->set('posts', $this->paginate());
    }

    /**
     * view method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function admin_view($id = null) {
        $this->Post->id = $id;
        if (!$this->Post->exists()) {
            throw new NotFoundException(__('Invalid post'));
        }
        $this->set('post', $this->Post->read(null, $id));
    }

    /**
     * add method
     *
     * @return void
     */
    public function admin_add() {
        $this->loadModel('Category');
        if ($this->request->is('post')) {
            $this->Post->create();
            $this->request->data['Post']['created_at'] = date("Y-m-d H:i:s");
            $this->request->data['Post']['updated_at'] = date("Y-m-d H:i:s");
            $this->request->data['Post']['created_by'] = $this->Auth->user('id');

            //update image sizes
            $this->loadModel('Parameter');
            $sizes = $this->Parameter->getPostImageSizes();
            $this->Post->updateImageSizes($this->Post->name, $sizes);

            //update type for post
            $category = $this->Category->findById($this->request->data['Post']['category_id']);
            if (isset($category)) {
                if ($category['Category']['type'] == Category::posts) {
                    $this->request->data['Post']['type'] = Post::news;
                }
            }

            if ($this->Post->save($this->request->data)) {
                $this->Session->setFlash(__('The post has been saved'), 'default', array(), 'good');
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The post could not be saved. Please, try again.'), 'default', array(), 'bad');
            }
        }
        $categories = $this->Post->Category->generateTreeList(null, null, null, "&raquo;&rarr;&nbsp;&nbsp;");
        $this->set(compact('categories'));
    }

    /**
     * edit method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function admin_edit($id = null) {
        $this->Post->id = $id;
        if (!$this->Post->exists()) {
            throw new NotFoundException(__('Invalid post'));
        }
        if ($this->request->is('post') || $this->request->is('put')) {
            $this->request->data['Post']['updated_at'] = date("Y-m-d H:i:s");

            //update image sizes
            $this->loadModel('Parameter');
            $sizes = $this->Parameter->getPostImageSizes();
            $this->Post->updateImageSizes($this->Post->name, $sizes);
            if ($this->Post->save($this->request->data)) {
                $this->Session->setFlash(__('The post has been saved'), 'default', array(), 'good');
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The post could not be saved. Please, try again.'), 'default', array(), 'bad');
            }
        } else {
            $this->request->data = $this->Post->read(null, $id);
        }
        $categories = $this->Post->Category->generateTreeList(null, null, null, "&raquo;&rarr;&nbsp;&nbsp;");
        $this->set(compact('categories'));
    }

    /**
     * delete method
     *
     * @throws MethodNotAllowedException
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function admin_delete($id = null) {
        if (!$this->request->is('post')) {
            throw new MethodNotAllowedException();
        }
        $this->Post->id = $id;
        if (!$this->Post->exists()) {
            throw new NotFoundException(__('Invalid post'));
        }
        if ($this->Post->delete()) {
            $this->Session->setFlash(__('Post deleted'), 'default', array(), 'good');
            $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash(__('Post was not deleted'), 'defaut', array(), 'bad');
        $this->redirect(array('action' => 'index'));
    }

    /**
     * admin_update_status
     */
    public function admin_updateStatus() {
        $this->Post->id = $_POST['id'];
        if (!$this->Post->exists()) {
            throw new NotFoundException(__('Invalid post'));
        }
        if ($this->request->is('post')) {
            $this->request->data['Post']['status'] = $_POST['status'];
            if ($this->Post->save($this->request->data)) {
                $this->Session->setFlash(__('The post has been saved'), 'default', array(), 'good');
            } else {
                $this->Session->setFlash(__('The post could not be saved. Please, try again.'), 'default', array(), 'bad');
            }
        }

        $this->Post->recursive = 0;
        $this->set('posts', $this->paginate());
        $this->render('/Elements/Backend/post_list', 'ajax');
    }

}
